﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EFFC.Net.Manager;
using EFFC.Net.Common;
using EFFC.Net.Resource;
using EFFC.Net.Data.DBData;
using EFFC.Net.Parameters;
using EFFC.Net.Returns;
using EFFC.Net.WebBuilder.WebParameters;
using EFFC.Net.WebBuilder.Public;
using EFFC.Net.WebBuilder.Parameters;
using EFFC.Net.Interface;

namespace EFFC.Net.WebBuilder.WebManage
{
    public class WebDefaultManage:WebManager
    {
        public override QueryReturn Query<T>(WebManageParameter p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                QueryReturn rtn = t.Query(p);
                if (p.IsOperatorLog)
                {
                    WebOperatorP op = new WebOperatorP();
                    op.User = p.LoginUserID;
                    op.AgentID = p.AgentUserID;
                    op.ActionID = p.ActionID;
                    op.At = t.GetType().Name + ".Query";
                    op.FunctionID = p.FunctionID;
                    op.When = DateTime.Now;
                    if (p.ContainName("UserItem") && ComFunc.nvl(p.GetValue("UserItem")) != null)
                    {
                        op.UseItem = p.GetValue<string>("UserItem");
                    }
                    else
                    {
                        op.UseItem = "查詢";
                    }
                    if (p.ContainName("SQL"))
                    {
                        op.SQL = p.GetValue<string>("SQL");
                    }
                    else
                    {
                        op.SQL = t.Dao.LastCommandSQL;
                    }
                    GlobalCommon.LogOperator.WriteOperator(op);
                }
                return rtn;

            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override bool Update<T>(WebManageParameter p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                bool rtn = t.Update(p);
                if (p.IsOperatorLog)
                {
                    WebOperatorP op = new WebOperatorP();
                    op.User = p.LoginUserID;
                    op.AgentID = p.AgentUserID;
                    op.ActionID = p.ActionID;
                    op.At = t.GetType().Name + ".Update";
                    op.FunctionID = p.FunctionID;
                    op.When = DateTime.Now;
                    if (p.ContainName("UserItem") && ComFunc.nvl(p.GetValue("UserItem")) != null)
                    {
                        op.UseItem = p.GetValue<string>("UserItem");
                    }
                    else
                    {
                        op.UseItem = "修改";
                    }
                    if (p.ContainName("SQL"))
                    {
                        op.SQL = p.GetValue<string>("SQL");
                    }
                    else
                    {
                        op.SQL = "";
                    }
                    GlobalCommon.LogOperator.WriteOperator(op);
                }
                return rtn;

            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return false;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override bool Delete<T>(WebManageParameter p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                bool rtn = t.Delete(p);
                if (p.IsOperatorLog)
                {
                    WebOperatorP op = new WebOperatorP();
                    op.User = p.LoginUserID;
                    op.AgentID = p.AgentUserID;
                    op.ActionID = p.ActionID;
                    op.At = t.GetType().Name + ".Delete";
                    op.FunctionID = p.FunctionID;
                    op.When = DateTime.Now;
                    if (p.ContainName("UserItem") && ComFunc.nvl(p.GetValue("UserItem")) != null)
                    {
                        op.UseItem = p.GetValue<string>("UserItem");
                    }
                    else
                    {
                        op.UseItem = "刪除";
                    }
                    if (p.ContainName("SQL"))
                    {
                        op.SQL = p.GetValue<string>("SQL");
                    }
                    else
                    {
                        op.SQL = "";
                    }
                    GlobalCommon.LogOperator.WriteOperator(op);
                }
                return rtn;

            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return false;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override bool Insert<T>(WebManageParameter p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                bool rtn = t.Insert(p);
                if (p.IsOperatorLog)
                {
                    WebOperatorP op = new WebOperatorP();
                    op.User = p.LoginUserID;
                    op.AgentID = p.AgentUserID;
                    op.ActionID = p.ActionID;
                    op.At = t.GetType().Name + ".Insert";
                    op.FunctionID = p.FunctionID;
                    op.When = DateTime.Now;
                    if (p.ContainName("UserItem") && ComFunc.nvl(p.GetValue("UserItem")) != null)
                    {
                        op.UseItem = p.GetValue<string>("UserItem");
                    }
                    else
                    {
                        op.UseItem = "新增";
                    }
                    if (p.ContainName("SQL"))
                    {
                        op.SQL = p.GetValue<string>("SQL");
                    }
                    else
                    {
                        op.SQL = "";
                    }
                    GlobalCommon.LogOperator.WriteOperator(op);
                }
                return rtn;

            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return false;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override QueryByPageReturn QueryByPage<T>(WebManageParameter p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                QueryByPageReturn rtn = new QueryByPageReturn();

                DBOParameterCollection sqlparameter = new DBOParameterCollection();
                t.Dao.Open(WebConfigString.DBConn);
                t.Dao.BeginTrans();
                //预执行sql
                string presql = t.QueryByPageSQL_Prepare(p, ref sqlparameter);
                if (presql != "")
                {
                    DBAP dbap = ADBAccess.GetDBParametersDefine();
                    dbap.SQL = presql;
                    if (sqlparameter.Count > 0)
                    {
                        dbap.SQL_Parameters = sqlparameter;
                    }
                    t.Dao.ExecuteNoQuery(dbap);
                }
                //查询sql
                sqlparameter.Clear();
                string sql = t.QueryByPageSQL_QueryByPage(p, ref sqlparameter);
                DBAPageP dbapp = ADBAccess.GetDBParamPageDefine();
                dbapp.Count_of_OnePage = p.PageSize == -1 ? WebConfigString.PageSize : p.PageSize;
                dbapp.CurrentPage = p.CurrentPage;
                dbapp.SQL = sql;
                if (sqlparameter.Count > 0)
                {
                    dbapp.SQL_Parameters = sqlparameter;
                }
                if (!string.IsNullOrEmpty(p.OrderBy))
                {
                    dbapp.OrderBy = p.OrderBy.ToString();
                }
                
                t.Dao.StartPageByCondition(dbapp);
                rtn.DataResult = t.Dao.GoToPage(p.GoToPage);
                rtn.TotalCount = t.Dao.RowCountByPage;
                rtn.CurrentPage = t.Dao.CurrentPage;
                rtn.TotalPage = t.Dao.TotalPage;
                //结尾sql执行
                sqlparameter.Clear();
                string endsql = t.QueryByPageSQL_End(p, ref sqlparameter);
                if (endsql != "")
                {
                    DBAP dbap = ADBAccess.GetDBParametersDefine();
                    dbap.SQL = endsql;
                    if (sqlparameter.Count > 0)
                    {
                        dbap.SQL_Parameters = sqlparameter;
                    }
                    t.Dao.ExecuteNoQuery(dbap);
                }

                t.Dao.Commit();
                //记录logOperator
                if (p.IsOperatorLog)
                {
                    //點擊查詢button有效，但翻頁不在記錄了，設個卡子，不然操作記錄會爆滿
                    if (ComFunc.nvl(p.GetValue("UserItem")) != "")
                    {
                        WebOperatorP op = new WebOperatorP();
                        op.User = p.LoginUserID;
                        op.AgentID = p.AgentUserID;
                        op.ActionID = p.ActionID;
                        op.At = t.GetType().Name + ".QueryByPage";
                        op.FunctionID = p.FunctionID;
                        op.When = DateTime.Now;
                        if (p.ContainName("UserItem") && ComFunc.nvl(p.GetValue("UserItem")) != "")
                        {
                            op.UseItem = p.GetValue<string>("UserItem");
                        }
                        else
                        {
                            op.UseItem = "查詢";
                        }
                        if (p.ContainName("SQL"))
                        {
                            op.SQL = p.GetValue<string>("SQL");
                        }
                        else
                        {
                            op.SQL = t.Dao.LastCommandSQL;
                        }
                        GlobalCommon.LogOperator.WriteOperator(op);
                    }
                }
                //返回数据
                return rtn;

            }
            catch (Exception ex)
            {
                if (t != null) t.Dao.RollBack();
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override WebLogicRtn Process<T>(WebManageParameter p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                WebLogicRtn rtn = t.Process(p);
                if (p.IsOperatorLog)
                {
                    WebOperatorP op = new WebOperatorP();
                    op.User = p.LoginUserID;
                    op.AgentID = p.AgentUserID;
                    op.ActionID = p.ActionID;
                    op.At = t.GetType().Name + ".Process";
                    op.FunctionID = p.FunctionID;
                    op.When = DateTime.Now;
                    if (p.ContainName("UserItem") && ComFunc.nvl(p.GetValue("UserItem")) != null)
                    {
                        op.UseItem = p.GetValue<string>("UserItem");
                    }
                    else
                    {
                        op.UseItem = "業務邏輯計算處理";
                    }
                    if (p.ContainName("SQL"))
                    {
                        op.SQL = p.GetValue<string>("SQL");
                    }
                    else
                    {
                        op.SQL = "";
                    }
                    GlobalCommon.LogOperator.WriteOperator(op);
                }
                return rtn;

            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override WebLogicCheckRtn Check<T>(WebManageParameter p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                WebLogicCheckRtn rtn = t.Check(p);
                if (p.IsOperatorLog)
                {
                    WebOperatorP op = new WebOperatorP();
                    op.User = p.LoginUserID;
                    op.AgentID = p.AgentUserID;
                    op.ActionID = p.ActionID;
                    op.At = t.GetType().Name + ".Check";
                    op.FunctionID = p.FunctionID;
                    op.When = DateTime.Now;
                    if (p.ContainName("UserItem") && ComFunc.nvl(p.GetValue("UserItem")) != null)
                    {
                        op.UseItem = p.GetValue<string>("UserItem");
                    }
                    else
                    {
                        op.UseItem = "業務規則檢核";
                    }
                    if (p.ContainName("SQL"))
                    {
                        op.SQL = p.GetValue<string>("SQL");
                    }
                    else
                    {
                        op.SQL = "";
                    }
                    GlobalCommon.LogOperator.WriteOperator(op);
                }
                return rtn;

            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }
        /// <summary>
        /// 获取公共参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="p"></param>
        /// <returns></returns>
        public override CommonLogicReturn GetValue<T>(params object[] p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                CommonLogicParameter clp = new CommonLogicParameter();
                for (int i = 0; i < p.Length; i++)
                {
                    clp.SetValue("P" + i, p[i]);
                }
                CommonLogicReturn rtn = t.GetValueBy(clp);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }
        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="p"></param>
        /// <returns></returns>
        public override IsExistsRtn IsExists<T>(ParametersStd p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                IsExistsRtn rtn = t.IsExists(p);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override FileUpLoadRtn UpLoadFile<T>(FileUpLoadParameters p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                FileUpLoadRtn rtn = t.UpLoad(p);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override DownLoadRtn DownLoad<T>(ParametersStd p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                DownLoadRtn rtn = t.DownLoad(p);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override RemoveAttachRtn RemoveFile<T>(ParametersStd p)
        {
            T t = default(T);
            try
            {
                t = System.Activator.CreateInstance<T>();
                RemoveAttachRtn rtn = t.RemoveFile(p);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }
    }
}
